package com.a21vianet.quincysx.demo.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class BookContentProvider extends ContentProvider {
    private static final String TAG = BookContentProvider.class.getSimpleName();

    public static final String AUTHORITY = "com.a21vianet.quincysx.demo.contentprovider" +
            ".BookContentProvider";

    public static final Uri BOOK_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/book");
    public static final Uri USER_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/user");

    public static final int BOOK_URI_CODE = 0;
    public static final int USER_URI_CODE = 1;

    public static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        URI_MATCHER.addURI(AUTHORITY, "book", BOOK_URI_CODE);
        URI_MATCHER.addURI(AUTHORITY, "user", USER_URI_CODE);
    }

    private Context mContext;
    private DBOpenHelper mDBOpenHelper;

    public BookContentProvider() {
    }

    private String getTableName(Uri uri) {
        String tableName = null;
        switch (URI_MATCHER.match(uri)) {
            case BOOK_URI_CODE:
                tableName = DBOpenHelper.BOOK_TABLE_NAME;
                break;
            case USER_URI_CODE:
                tableName = DBOpenHelper.USER_TABLE_NAME;
                break;
            default:
        }
        return tableName;
    }

    @Override
    public String getType(Uri uri) {
        return "*/*";
    }

    @Override
    public boolean onCreate() {
        mContext = getContext();
        initProviderData();
        return true;
    }

    private void initProviderData() {
        mDBOpenHelper = new DBOpenHelper(getContext());
        SQLiteDatabase DB = mDBOpenHelper.getWritableDatabase();
        DB.execSQL("DELETE FROM " + DBOpenHelper.BOOK_TABLE_NAME);
        DB.execSQL("DELETE FROM " + DBOpenHelper.USER_TABLE_NAME);

        DB.execSQL("INSERT INTO " + DBOpenHelper.BOOK_TABLE_NAME + " values(3,'Android',23.3)");
        DB.execSQL("INSERT INTO " + DBOpenHelper.BOOK_TABLE_NAME + " values(4,'iOS',13.3)");
        DB.execSQL("INSERT INTO " + DBOpenHelper.BOOK_TABLE_NAME + " values(5,'HTML5',19.9)");

        DB.execSQL("INSERT INTO " + DBOpenHelper.USER_TABLE_NAME + " values(3,'张三',1)");
        DB.execSQL("INSERT INTO " + DBOpenHelper.USER_TABLE_NAME + " values(4,'王五',1)");
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        String tableName = getTableName(uri);
        int count = mDBOpenHelper.getWritableDatabase().delete(tableName,
                selection, selectionArgs);
        if (count > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
        }
        return count;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        String tableName = getTableName(uri);
        long count = mDBOpenHelper.getWritableDatabase().insert(tableName,
                null, values);
        mContext.getContentResolver().notifyChange(uri, null);
        return uri;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        String tableName = getTableName(uri);
        return mDBOpenHelper.getReadableDatabase().query(tableName,
                projection, selection, selectionArgs, null, null, sortOrder, null);
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        String tableName = getTableName(uri);
        int count = mDBOpenHelper.getWritableDatabase().update(tableName,
                values, selection, selectionArgs);
        if (count > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
        }
        return count;
    }
}
